package com.j.thetabk.myapplication;

import android.graphics.ImageFormat;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;

public class MainActivity extends AppCompatActivity implements SurfaceHolder.Callback{


    SurfaceView sv=null;
    Camera camera;
    SurfaceHolder mHolder;

    Frame2Bitmap f2b;

    String savename="";

    private Camera.PreviewCallback cpc= new Camera.PreviewCallback() {
        @Override
        public void onPreviewFrame(byte[] data, Camera camera) {
            Log.v("onPreviewFrame", "callback"+data.length);

            //                            new Frame(data).SaveRawBitmap(Environment.getExternalStorageDirectory().getAbsolutePath()+"/frm.png",640,480*3/2);

            synchronized (savename)
            {
                if (!savename.equals("")) {
                    setTitle(savename);
                    try {
                        Frame.SaveBitmap(Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + savename + ".png", f2b.Convert(data));
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    savename = "";
                }
            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        sv=(SurfaceView)findViewById(R.id.surfaceView);

        mHolder = sv.getHolder();
        mHolder.addCallback(this);


        f2b=new Frame2Bitmap(this, 1920, 1080);


        new Thread(){
            @Override
            public void run() {
                super.run();

                try {
                    Thread.sleep(1500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                for(int i=0;i<9*9;i++)
                {
                    synchronized (savename)
                    {
                        savename="img"+i;
                    }
                    try {
                        Thread.sleep(4000+5000/3);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

                finishAffinity();
            }
        }.start();

    }


    @Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
        try{
            camera = Camera.open(0);
        } catch (RuntimeException ex){
//                Toast.makeText(ctx, getString(R.string.camera_not_found), Toast.LENGTH_LONG).show();
        }
        try {
            if (camera != null) {

                Camera.Parameters params = camera.getParameters();
                params.setPreviewSize(1920, 1080);
                camera.setParameters(params);

                Log.v("surfaceCreated", "camera preview size " + params.getPreviewSize().width + "x" + params.getPreviewSize().height +
                        "x"+ImageFormat.getBitsPerPixel(params.getPreviewFormat())+"bit(" + params.getPreviewFormat() + ")");

                camera.setPreviewDisplay(surfaceHolder);
                camera.setPreviewCallback(cpc);
            }
        } catch (IOException exception) {
//            Log.e(TAG, "IOException caused by setPreviewDisplay()", exception);
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
        if(camera != null) {
            camera.startPreview();
        }
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
        // Surface will be destroyed when we return, so stop the preview.
        if (camera != null) {
            camera.stopPreview();
            camera.setPreviewCallback(null);
            camera.release();
            camera = null;
        }
    }



}
